/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ssau.entities;

import java.io.Serializable;
import org.jdom.Element;
import ru.ssau.lib.XMLizable;
import ru.ssau.utils.XMLHelper;

public class AuthorizationInfo implements Serializable, XMLizable {

    private int id;
    private int userId;
    private String login;
    private String password;

    public AuthorizationInfo(int id, int userId, String login, String password) {
        this.id = id;
        this.userId = userId;
        this.login = login;
        this.password = password;
    }

    public Element toXMLElement() {
        Element root = new Element("AuthorizationInfo");
        XMLHelper.addElement(root, "id", id);
        XMLHelper.addElement(root, "userId", userId);
        XMLHelper.addElement(root, "login", login);
        XMLHelper.addElement(root, "password", password);
        return root;
    }

    public void loadFromXMLElement(Element data) {
        Element temp = null;
        if ((temp = data.getChild("id")) != null) {
            setId(Integer.valueOf(temp.getValue()));
        }
        if ((temp = data.getChild("userId")) != null) {
            setUserId(Integer.valueOf(temp.getValue()));
        }
        if ((temp = data.getChild("login")) != null) {
            setLogin(temp.getValue());
        }
        if ((temp = data.getChild("password")) != null) {
            setPassword(temp.getValue());
        }
    }

    public AuthorizationInfo() {
        login = "";
        password = "";
    }

    public AuthorizationInfo(String login, String password) {
        this.login = login;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != AuthorizationInfo.class) {
            return false;
        }
        AuthorizationInfo other = (AuthorizationInfo) obj;
        return (id == other.getId() && userId == other.userId
                && login.equals(other.login) && password.equals(other.password));
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 29 * hash + this.id;
        hash = 29 * hash + this.userId;
        hash = 29 * hash + (this.login != null ? this.login.hashCode() : 0);
        hash = 29 * hash + (this.password != null ? this.password.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getName());
        sb.append(": [id=");
        sb.append(id);
        sb.append(", userId=");
        sb.append(userId);
        sb.append(", login=");
        sb.append(login);
        sb.append(", password=");
        sb.append(password);
        sb.append("]");
        return sb.toString();
    }
}
